package com.sth.edu.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sth.commonutils.R;
import com.sth.edu.entity.Course;
import com.sth.edu.entity.Teacher;
import com.sth.edu.service.CourseService;
import com.sth.edu.service.TeacherService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/teacherfront")
@CrossOrigin
@Api(description = "前台页面的讲师")
public class TeacherFrontController {

    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CourseService courseService;

    //分页查询讲师
    @PostMapping("getTeacherFrontList/{page}/{limit}")
    public R getTeacherFrontList(@PathVariable long page, @PathVariable long limit) {
        Page<Teacher> pageTecher = new Page<>(page, limit);
        Map<String, Object> map = teacherService.getTeacherFrontList(pageTecher);
        // 返回所有分页数据
        return R.ok().data(map);
    }

    // 2. 讲师详情查询
    @GetMapping("getTeacherFrontInfo/{teacherId}")
    public R getTeacherFrontInfo(@PathVariable String teacherId) {
        // 1.根据讲师id 查询教师
        Teacher teacher = teacherService.getById(teacherId);
        // 2.根据讲师id 查询课程
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id", teacherId);
        List<Course> courseList = courseService.list(queryWrapper);
        return R.ok().data("teacher", teacher).data("courseList", courseList);
    }
}
